package com.egene.tw.data;

import com.egene.tw.data.entity.DeviceSetting;
import com.egene.tw.data.entity.HeartData;
import com.egene.tw.data.entity.KeyValues;

import java.util.List;

import io.realm.Realm;
import io.realm.Sort;

public class RealmHelper {

    ///////////////////////////////////////////////////////////////////////////
    // 当前登录账号
    ///////////////////////////////////////////////////////////////////////////
    public static void saveCurrUserId(String id) {
        Realm realm = Realm.getDefaultInstance();
        try {
            KeyValues keyValues = new KeyValues();
            keyValues.setKey("logged_id");
            keyValues.setValue(id);

            realm.beginTransaction();
            realm.copyToRealmOrUpdate(keyValues);
            realm.commitTransaction();
        } finally {
            realm.close();
        }

    }

    public static String getCurrUserId(Realm realm) {
        KeyValues keyValues = realm.where(KeyValues.class).equalTo("key", "logged_id").findFirst();
        if (keyValues == null) {
            return "";
        }
        return keyValues.getValue();
    }

    public static void delCurrUserId(Realm realm) {
        KeyValues keyValues = realm.where(KeyValues.class).equalTo("key", "logged_id").findFirst();
        if (keyValues == null) {
            return;
        }
        realm.beginTransaction();
        keyValues.deleteFromRealm();
        realm.commitTransaction();
    }

    public static List<HeartData> heartDataList(Realm realm) {
        String userId = getCurrUserId(realm);
        List<HeartData> list = realm.where(HeartData.class).equalTo("userId", userId).findAllSorted("time", Sort.DESCENDING);
        return realm.copyFromRealm(list);
    }

    public static void saveSettings(DeviceSetting setting) {
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.beginTransaction();
            realm.copyToRealmOrUpdate(setting);
            realm.commitTransaction();
        } finally {
            realm.close();
        }
    }

    public static DeviceSetting loadSettings() {
        DeviceSetting deviceSetting;
        Realm realm = Realm.getDefaultInstance();
        try {
            deviceSetting = realm.where(DeviceSetting.class).equalTo("id", "deviceSetting").findFirst();
            if (deviceSetting != null) {
                deviceSetting = realm.copyFromRealm(deviceSetting);
            } else {
                deviceSetting = new DeviceSetting();
                deviceSetting.setId("deviceSetting");
                deviceSetting.setDisfangdiu(true);
                deviceSetting.setDisremind(true);
                deviceSetting.setDislight(true);
                deviceSetting.setDisdsMeasure(true);


                deviceSetting.setBloodHeight(180);
                deviceSetting.setBloodLow(120);
                deviceSetting.setTargetStep(8000);
            }
        } finally {
            realm.close();
        }
        return deviceSetting;
    }

    ///////////////////////////////////////////////////////////////////////////
    // 蓝牙
    ///////////////////////////////////////////////////////////////////////////
    public static void saveDeviceAddress(String address) {
        Realm realm = Realm.getDefaultInstance();
        try {
            KeyValues keyValues = new KeyValues();
            keyValues.setKey("bind_address");
            keyValues.setValue(address);

            realm.beginTransaction();
            realm.copyToRealmOrUpdate(keyValues);
            realm.commitTransaction();
        } finally {
            realm.close();
        }

    }

    public static String getDeviceAddress() {
        String value = "";
        Realm realm = Realm.getDefaultInstance();
        try {
            KeyValues keyValues = realm.where(KeyValues.class).equalTo("key", "bind_address").findFirst();
            if (keyValues != null) {
                value = keyValues.getValue();
            }
        } finally {
            realm.close();
        }
        return value;
    }


    public static void saveDeviceName(String name) {
        Realm realm = Realm.getDefaultInstance();
        try {
            KeyValues keyValues = new KeyValues();
            keyValues.setKey("bind_name");
            keyValues.setValue(name);

            realm.beginTransaction();
            realm.copyToRealmOrUpdate(keyValues);
            realm.commitTransaction();
        } finally {
            realm.close();
        }

    }


    public static String getDeviceName() {
        String value = "";
        Realm realm = Realm.getDefaultInstance();
        try {
            KeyValues keyValues = realm.where(KeyValues.class).equalTo("key", "bind_name").findFirst();
            if (keyValues != null) {
                value = keyValues.getValue();
            }
        } finally {
            realm.close();
        }
        return value;
    }
}
